﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SACM.Base;
using SACM.Common.Constantes;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data.SqlTypes;
using System.Data.OleDb;
using SACM.Utils.Extensions;
using GandA.Component.Log;





namespace SACM.PageConsultorio
{
    public partial class AdminAgenda : BasePage
    {
        private SqlDataAdapter da;
        protected void Page_Init(object sender, EventArgs e)
        {
            IdItem = 3;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //toolbar.BtnExportar.Click += new EventHandler(BtnExportar_Click);
            toolbar.BtnAgregar.Attributes.Add("onclick", "return OpenRegistro();");
            if (!Page.IsPostBack)
            {
                Master.TituloPagina = "Administración de Agenda";

               
                int idEmpresa = SACM.Common.Util.Convert.ToInt(SessionEmpresa.IdEmpresa);
                int idSucursal = SACM.Common.Util.Convert.ToInt(SessionEmpresa.IdSucursal);
                cmbDoc.DoctoresCmb();
                CmbPaciente.PacientesCmb();

                TxtFecha.Text = DateTime.Now.ToString("dd/MM/yyyy");
                

               
            }

            
        }


        protected void BtnExportar_Click(object sender, EventArgs e)
        {
            if (gridAgenda.Rows.Count > 0)
            {
                gridAgenda.AllowPaging = false;
                CargaGrid();
                gridAgenda.ToExportExcel("Agenda");
                gridAgenda.AllowPaging = true;
                CargaGrid();
            }
        }



        protected void gridAgenda_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                //string[] sParams;
                //switch (e.CommandName)
                //{
                //    case "cmdModificar":
                //        sParams = e.CommandArgument.ToString().Split(',');
                //        SaveFilters();
                //        Response.Redirect(ResolveUrl("CatDoctoresRegistro.aspx?IdDoctor=" + sParams[0]), false);
                //        break;
                //}
            }
            catch (Exception ex)
            {
                Master.alert(ex.Message);
            }
        }

        protected void gridAgenda_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                gridAgenda.PageIndex = e.NewPageIndex;
                CargaGrid();
            }
            catch (Exception ex)
            {
                Master.alert(ex.Message);
            }
        }



        protected void btnElimina_Click(object sender, EventArgs e)
        {
            try
            {
                int id = Common.Util.Convert.ToNInt(hdnId.Value).GetValueOrDefault(-1);
                EliminaAgenda(id);
            }
            catch (Exception ex)
            {
                Master.alert(ex.Message);
            }
        }

        protected void BtnBuscar_Click(object sender, EventArgs e)
        {
            try
            {
                CargaGrid();
            }
            catch (Exception ex)
            {
                Master.alert(ex.Message);
            }
        }

        #region Metodos

        private void CargaGrid()
        {
            ArrayList parametros = new ArrayList();
            ConsultaServiceReference.ConsultaServiceClient servicio = new ConsultaServiceReference.ConsultaServiceClient();
            //Parametros
            parametros.Add(CreaParametroConsulta("@pnIdPaciente", CmbPaciente.SelectedValue, ConsultaServiceReference.Parameter.vEnumType.Int));
            parametros.Add(CreaParametroConsulta("@pnIdDoctor", cmbDoc.SelectedValue, ConsultaServiceReference.Parameter.vEnumType.Int));
            parametros.Add(CreaParametroConsulta("@ptFechaCita", TxtFecha.Text, ConsultaServiceReference.Parameter.vEnumType.DateTime));
            parametros.Add(CreaParametroConsulta("@pnIdEmpresa", SessionEmpresa.IdEmpresa, ConsultaServiceReference.Parameter.vEnumType.Int));
            parametros.Add(CreaParametroConsulta("@pnIdSucursal", SessionEmpresa.IdSucursal, ConsultaServiceReference.Parameter.vEnumType.Int));
            ConsultaServiceReference.Parameter[] prms = new ConsultaServiceReference.Parameter[parametros.Count];
            parametros.CopyTo(prms);
            //Ejecuta consulta
            gridAgenda.DataSource = servicio.EjecutaConsultaConParametros(General.SACM + ".spObtenCitas", prms);
            gridAgenda.DataBind();
        }

        private void EliminaAgenda(int? idcita)
        {
            //try
            //{
            //    DoctorServiceReference.DoctorServiceClient servicio = new DoctorServiceReference.DoctorServiceClient();
            //    DoctorServiceReference.Doctor obj = new DoctorServiceReference.Doctor();
            //    obj.IdDoctor = idDoctor;
            //    obj.IdEmpresa = SessionEmpresa.IdEmpresa;
            //    obj.IdSucursal = SessionEmpresa.IdSucursal;
            //    obj.IdUsuarioMod = SessionUsuario.IdUsuario;
            //    obj.IpMod = Request.UserHostAddress;
            //    servicio.Delete(obj);
            //    CargaGrid();
            //}
            //catch (Exception ex)
         
            //{
            //    Master.alert(ex.Message);
            //}
        }


        #endregion

    }
}